namespace SubhadraSolutions.Sharp.Logging.LogSubscribers.EventLog
{
    using System.Collections.Generic;
    using System.Diagnostics;

    public class EventLogLogSubscriber : AbstractLogSubscriber
    {
        #region Properties

        public string EventSource
        {
            get;
            set;
        }

        public string LogName
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        protected override void initializeProtected()
        {
            base.initializeProtected();
            if (!System.Diagnostics.EventLog.SourceExists(EventSource))
            {
                System.Diagnostics.EventLog.CreateEventSource(EventSource, LogName);
            }
        }

        protected override void publishLogs(IEnumerable<LogStoreItem> e)
        {
            foreach (LogStoreItem item in e)
            {
                var entry = item.LogEntry as EventLogItem;
                if (entry != null)
                {
                    System.Diagnostics.EventLog.WriteEntry(EventSource, entry.Message,
                        entry.EventSeverity,
                        entry.EventID, entry.Category, entry.RawData);
                }
                else
                {
                    EventLogEntryType entryType = LogItemSeverityHelper.Convert(item.Severity);
                    System.Diagnostics.EventLog.WriteEntry(EventSource, item.ToString(), entryType);
                }
            }
        }

        #endregion Methods
    }
}